<?php


namespace app\api\controller;


use app\admin\model\ListContent;
use app\admin\model\User;
use think\Controller;
use think\Request;

class ActiveOrder extends Controller
{

    public function activeOrderList(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }
        $activeOrder = new \app\admin\model\ActiveOrder();
        $where['id'] = ['neq',0];
        $where['user_id'] = $data['user_id'];
        $rs = $activeOrder->getAll($where);
        foreach($rs as $j){
            $j['info'] = json_decode($j['info']);
        }

        $list = [];
        $list['dhs'] = [];
        $list['wfk'] = [];
        $list['efk'] = [];

        foreach($rs as $j){

            if($j['status'] == 0){
                $list['dhs'][] = $j;
            }

            if($j['status'] == 1){
                $list['wfk'][] = $j;
            }

            if($j['status'] == 2){
                $list['efk'][] = $j;
            }
        }
        if($rs){
            $res['data']['code'] = 1;
            $res['data']['list'] = $list;
        }else{
            $res['data']['code'] = 0;
        }

        $res['status'] = 200;
        echo json_encode($res);
    }

    public function addActiveOrder(Request $request){
        $res = [];
        $data = $request->post();

        if(!isset($data['cart_id']) or !isset($data['selected_id']) or !isset($data['pr_type']) or !isset($data['pr_id']) or !isset($data['user_id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        //查询是否有此酒店
        $listContent = new ListContent();
        $selectedWhere['id'] = $data['selected_id'];
        $selected = $listContent->getOne($selectedWhere);
        if(!$selected){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '酒店不存在';
            $res['status'] = 200;
            echo json_encode($res);die;
        }
        //查询是否有此活动
        $active = $this->getActive($data['pr_type'],$data['pr_id']);
        if(!$active){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '活动不存在';
            $res['status'] = 200;
            echo json_encode($res);die;
        }

        $user = new User();
        $userWhere['id'] = $data['user_id'];
        $userInfo = $user->getOne($userWhere);
        if(!$userInfo){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '用户不存在';
            $res['status'] = 200;
            echo json_encode($res);die;
        }
        $en['active'] = $active;
        $en['selected'] = $selected;
        $en = json_encode($en);

        $activeOrder = new \app\admin\model\ActiveOrder();

        $addData['user_id'] = $data['user_id'];
        $addData['order'] = time().rand(000000,999999);
        $addData['info'] = $en;
        $addData['status'] = 0;
        $addData['price'] = 0.00;
        $addData['mark'] = '';
        $addData['create_time'] = time();

        $rs = $activeOrder->add($addData);
        if($rs){
            $shopCart = new \app\admin\model\ShopCart();
            $delId = explode(',',$data['cart_id']);
            foreach ($delId as $j){
                $delWhere['id'] = $j;
                $shopCart->del($delWhere);
            }

            $res['data']['code'] = 1;
            $res['data']['msg'] = '下单成功';
        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = '下单失败';
        }
        $res['status'] = 200;
        echo json_encode($res);die;
    }

    public function getActive($pr_type,$pr_id){
        if(empty($pr_type) or empty($pr_id)){
            return false;
        }else{
            switch ($pr_type){
                case "1":
                    $db = new \app\admin\model\Foreknowledge();
                    break;
                case "2":
                    $db = new \app\admin\model\Recorded();
                    break;
                case "3":
                    $db = new \app\admin\model\LargerActive();
                    break;
                default:
                    $db = false;
            }
            if(!$db){
                return false;
            }
            $where['id'] = $pr_id;
            $rs = $db->getOne($where);
            if(!$rs){
                return false;
            }else{
                return $rs;
            }
        }
    }
}